/**
 * @file app\utils\inc\storage.h
 *
 * Copyright (C) 2023
 *
 * storage.h is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author HinsShum hinsshum@qq.com
 *
 * @encoding utf-8
 */
#ifndef __STORAGE_H
#define __STORAGE_H

#ifdef __cplusplus
extern "C"
{
#endif

/*---------- includes ----------*/
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include "options.h"
#include "protocol_mobus_def.h"
/*---------- macro ----------*/
#define CONFIG_STORAGE_MOBUS_CHANNEL_NUMS               (1)
#define CONFIG_STORAGE_MOBUS_GROUP_NUMS                 (10)

    typedef enum {
        STORAGE_AREA_LIGHT_MODE,
        STORAGE_AREA_LIANPAN,
        STORAGE_AREA_MOBUS_GROUPS,
        STORAGE_AREA_AMBIENT,
        STORAGE_AREA_DEVICE,
        STORAGE_AREA_MOTOR,
        STORAGE_AREA_PIR,
        STORAGE_AREA_KEY,
        STORAGE_AREA_RELAY,
    } storage_area_t;
    typedef enum
    {
        _led_level_1 = 1,
        _led_level_2,
        _led_level_3,
        _led_level_4,
        _led_level_5,
        _led_level_6,
        _led_level_7,
        _led_level_8,
        _led_level_9,
        _led_level_10,
        _led_value_zero = 0,
        _led_value_min = 2,
        _led_value_mid = 30,
        _led_value_max = 100
    }_led_level;
    typedef enum
    {
        full_bright = 1,
        mid_bright,
        low_bright,
        die_bright,
        auto_bright,
        pir_bright,
        no_spuse = 0xff
    }_led_mode;
    typedef enum
    {
        _motor_off = 0,
        _motor_level_1 = 1,
        _motor_level_2,
        _motor_level_3,
        _motor_level_4,
        _motor_level_5,
        _motor_level_6,
        _motor_level_7,
        _motor_level_8,
        _motor_level_9
    }_motor_level;
    /*---------- variable prototype ----------*/
    //丽思卡尔顿酒店定制面板
    typedef struct
    {
        uint16_t background;
        uint8_t LisicarD_sys;
    }LisicarD;
    struct storage_led_struct {
        _led_mode led_mode;
        _led_level led_level;
        LisicarD LisicarD_struct;
        uint8_t assign_bright;
    };
    struct storage_lianpai_struct {
        uint8_t lianpai_mode_num;
        uint8_t lianpai_mode_max;
    };

    struct rgb_struct
    {
        uint8_t R;
        uint8_t G;
        uint8_t B;
        uint8_t C;
        uint8_t W;
    };
    struct storage_ambient_struct {

        struct rgb_struct temp_rgb_struct;
        uint8_t hsi;
        uint16_t cw_temperature;
        uint8_t brightness;
        uint8_t beiguang_meeting_ambient_percentage;
    };


    struct storage_mobus_device_info {
        uint8_t native_address;                             /*<< mobus native address: 1-254 */
        mobus_device_type_t device_type;                    /*<< mobus device type, see moorgen documents for detail */
        uint8_t channel_nums;
        uint16_t channel_type[CONFIG_STORAGE_MOBUS_CHANNEL_NUMS];
    };
    struct storage_mobus_device {
        struct storage_mobus_device_info device;
        uint8_t sn[23];
        uint8_t factory_reset_flag;
    };

    struct storage_motor_struct {
        _motor_level left_motor_lever;
        uint8_t left_motor_time;
        uint8_t left_shock_proofness;
        _motor_level right_motor_lever;
        uint8_t right_motor_time;
        uint8_t right_shock_proofness;
    };
    struct storage_pir_struct {
        uint8_t pir_enanle_flag;
        uint8_t pir_keylight_born_time;
        uint8_t pir_keylight_keep_time;
        uint8_t pir_keylight_die_time;
        uint8_t pir_distance;
    };
    typedef struct __PANNEL_ITEM
    {
        uint8_t flag;
        uint8_t type;
        uint8_t tg_id;
        uint8_t datalen;
        uint8_t buf[1];
        uint8_t key_onoff;
    }_PANNEL_ITEM;
    struct storage_key_struct {
        uint16_t dis_en_flag;
        _PANNEL_ITEM item[16];
    };
    struct storage_relay_struct {
        uint8_t relay_sta;
    };
    struct storage_mobus_group_info {
        uint16_t uid;                                       /*<< mobus group uid */
        uint8_t bits;                                       /*<< mobus channel bit */
    };
    struct storage_mobus_groups_info {
        struct storage_mobus_group_info groups[CONFIG_STORAGE_MOBUS_GROUP_NUMS];
    };
    /*---------- type define ----------*/
    typedef enum {
        LED_MODE_START = 1,
        LED_MODE_CLEAN,
        LED_MODE_LOCAL,
        LED_MODE_UPDATA,
        LED_MODE_WORK,
    } dev_led_mode_enum;

    typedef struct {
        struct {
            uint16_t s1_led_sat : 1;
            uint16_t s2_led_sat : 1;
            uint16_t s3_led_sat : 1;
            uint16_t s4_led_sat : 1;
            uint16_t s5_led_sat : 1;
            uint16_t s6_led_sat : 1;
            uint16_t s7_led_sat : 1;
            uint16_t s8_led_sat : 1;

            uint16_t backgroud_led : 1;
            uint16_t null_1 : 1;
            uint16_t null_2 : 1;
            uint16_t null_3 : 1;
            uint16_t s9_led_sat : 1;
            uint16_t s10_led_sat : 1;
            uint16_t s11_led_sat : 1;
            uint16_t s12_led_sat : 1;

            uint8_t autoplay_mode : 1;
            uint8_t net_mode : 1;
            uint8_t key_voluntary_mode : 1;
            uint8_t pir_enable_flag : 1;
            uint8_t ledx_dlydie_flag : 1;
            uint8_t press_up_send_enable : 1;
            uint8_t pir_keep_flag : 1;

        }LisicarD_Temp;
        struct {
            uint8_t led_mode_clean_times;
            dev_led_mode_enum dev_work_mode;//1 start_on  2 clean  3 local 

            uint8_t	 led1_toogle_flag;
            uint8_t	 led2_toogle_flag;
            uint8_t	 led3_toogle_flag;
            uint8_t	 led4_toogle_flag;
            uint8_t	 led5_toogle_flag;
            uint8_t	 led6_toogle_flag;
            uint8_t	 led7_toogle_flag;
            uint8_t	 led8_toogle_flag;
            uint8_t	 led9_toogle_flag;
            uint8_t	 led10_toogle_flag;
            uint8_t	 led11_toogle_flag;
            uint8_t	 led12_toogle_flag;
            uint8_t	 led13_toogle_flag;
            uint8_t	 led14_toogle_flag;
            uint8_t	 led15_toogle_flag;
            uint8_t	 led16_toogle_flag;
            uint8_t	 led1_blink_times;
            uint8_t	 led2_blink_times;
            uint8_t	 led3_blink_times;
            uint8_t	 led4_blink_times;
            uint8_t	 led5_blink_times;
            uint8_t	 led6_blink_times;
            uint8_t	 led7_blink_times;
            uint8_t	 led8_blink_times;
            uint8_t	 led9_blink_times;
            uint8_t	 led10_blink_times;
            uint8_t	 led11_blink_times;
            uint8_t	 led12_blink_times;
            uint8_t	 led13_blink_times;
            uint8_t	 led14_blink_times;
            uint8_t	 led15_blink_times;
            uint8_t	 led16_blink_times;
            uint16_t led1_push : 1;
            uint16_t led2_push : 1;
            uint16_t led3_push : 1;
            uint16_t led4_push : 1;
            uint16_t led5_push : 1;
            uint16_t led6_push : 1;
            uint16_t led7_push : 1;
            uint16_t led8_push : 1;
            uint16_t led9_push : 1;
            uint16_t led10_push : 1;
            uint16_t led11_push : 1;
            uint16_t led12_push : 1;
            uint16_t led13_push : 1;
            uint16_t led14_push : 1;
            uint16_t led15_push : 1;
            uint16_t led16_push : 1;
        }led_temp_variable;
        struct {
            uint8_t relay_actionflag[4];
            uint8_t occupyflag[4];
            uint8_t relay_out[4];
        }temp_relay;
        struct {
            uint8_t key_num;
        }temp_key;
        uint8_t allocate_adr_dly;
    }temp_variable;

    /*---------- function prototype ----------*/
    extern temp_variable no_storage;
    extern bool storage_init(void);
    extern void* storage_get(storage_area_t area);
    extern bool storage_set(storage_area_t area, void* value, uint32_t size);
    extern bool storage_restore(storage_area_t area);

#ifdef __cplusplus
}
#endif
#endif /* __STORAGE_H */
